package com.leetcode.algorithm.y22.m09.w2;

/**
 * 2078. 两栋颜色不同且距离最远的房子
 * 
 * https://leetcode.cn/problems/two-furthest-houses-with-different-colors/
 * 
 * @author jie.deng
 *
 */
class Question2078Solution01 {
	
	public int maxDistance(int[] colors) {
		int len = colors.length;
		int idx = len - 1;
		int color = colors[0];
		while (idx > 0 && color == colors[idx]) {
			idx--;
		}
		if (idx == len - 1) {
			return idx;
		}
		int distance = idx;
		color = colors[len - 1];
		idx = 0;
		while (idx < len && color == colors[idx]) {
			idx++;
		}
		return Math.max(distance, len - 1 - idx);
	}
    
}